package com.humngyang.com.hmy.study.mywx.service;

import com.humngyang.com.hmy.study.mywx.WeChatUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class ValidateService {

    public String validate(String signature, String timestamp, String nonce, String echostr) {
        //1）将token、timestamp、nonce三个参数进行字典序排序
        String[] arr = {WeChatUtils.TOKEN, timestamp, nonce};
        Arrays.sort(arr);
        //将三个参数字符串拼接成一个字符串进行sha1加密
        StringBuilder sb = new StringBuilder();
        for (String tem : arr) {
            sb.append(tem);
        }
        //自己加密的签名
        String mysignature = WeChatUtils.sha1(sb.toString());
        if (mysignature.equals(signature)) {
            System.out.println("接入成功");
            return echostr;
        }
        System.out.println("接入失败");
        return null;
    }
}
